package com.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.Employee;
import com.pojo.LeaveApply;
import com.pojo.OutApply;
import com.pojo.view.Attendance;
import com.pojo.view.LeaveApplyCheck;
import com.pojo.view.OutApplyCheck;
import com.service.DepartmentService;
import com.service.EmployeeService;
import com.service.LeaveApplyService;
import com.service.OutApplyService;
import com.service.impl.DepartmentServiceImpl;
import com.service.impl.EmployeeServiceImpl;
import com.service.impl.LeaveApplyServiceImpl;
import com.service.impl.OutApplyServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/empCheckInfoServlet/*")
public class EmpCheckInfoServlet extends BaseServlet {

    EmployeeService employeeService = new EmployeeServiceImpl();
    LeaveApplyService leaveApplyService = new LeaveApplyServiceImpl();
    OutApplyService outApplyService = new OutApplyServiceImpl();
    DepartmentService departmentService = new DepartmentServiceImpl();

    /**
     * 查看考勤情况
     * API接口说明
     * POST方法 接受username参数
     * {"username":"张三"}
     * 返回结果示例：{"annualLeaveRemain":15,"leaveTotal":3,"outTotal":2}
     */
    public void checkAttendance(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String username = jsonObject.get("username").toString();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");

        try{
            int annualLeaveRemain = employeeService.getAnnualLeaveRemainByUsername(username);
            int leaveTotal = employeeService.getLeaveTotalByUsername(username);
            int outTotal = employeeService.getOutTotalByUsername(username);

            Attendance attendance = new Attendance();
            attendance.setAnnualLeaveRemain(annualLeaveRemain);
            attendance.setLeaveTotal(leaveTotal);
            attendance.setOutTotal(outTotal);

            String jsonString = JSON.toJSONString(attendance);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
        catch (Exception e){
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write("没有对应的用户");
        }

    }


    /**
     * 查看请假申请
     * API接口说明
     * POST方法 参数username
     * {"username":"张三"}
     * 返回结果示例:
     * [
     * {
     * "applyTime": "2022-05-13 15:15:45.0",
     * "department": "技术部",
     * "endTime": "2022-05-19 15:00:00.0",
     * "leaveApplyId": 1,
     * "leaveReason": "朋友白事",
     * "name": "zhangsan",
     * "startTime": "2022-05-18 09:00:00.0",
     * "workId": "100005"
     * }
     * ]
     */
    public void checkLeaveApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String username = jsonObject.get("username").toString();


        Employee employee = employeeService.getEmployeeByUsername(username);
        String name = employee.getName();
        String workId = employee.getWorkId();
        Integer deptId = employee.getDeptId();
        String deptName = departmentService.selectDeptNameById(deptId);

        List<Integer> leaveApplyIdList = leaveApplyService.getLeaveApplyIdListByUsername(username);

        List<LeaveApplyCheck> leaveApplyCheckList = new ArrayList<>();
        for (Integer leaveApplyId : leaveApplyIdList) {
            LeaveApplyCheck leaveApplyCheck = new LeaveApplyCheck();
            LeaveApply leaveApply = leaveApplyService.getLeaveApplyByLeaveApplyId(leaveApplyId);
            String applyTime = leaveApply.getApplyTime();
            String startTime = leaveApply.getStartTime();
            String endTime = leaveApply.getEndTime();
            String leaveReason = leaveApply.getLeaveReason();
            String leaveCategory = leaveApply.getLeaveCategory();

            leaveApplyCheck.setLeaveApplyId(leaveApplyId);
            leaveApplyCheck.setName(name);
            leaveApplyCheck.setWorkId(workId);
            leaveApplyCheck.setDepartment(deptName);
            leaveApplyCheck.setApplyTime(applyTime);
            leaveApplyCheck.setStartTime(startTime);
            leaveApplyCheck.setEndTime(endTime);
            leaveApplyCheck.setLeaveReason(leaveReason);

            leaveApplyCheckList.add(leaveApplyCheck);
        }

        String jsonString = JSON.toJSONString(leaveApplyCheckList);

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


    /**
     * 查看外出申请
     * API接口说明
     * POST方法 参数username
     * {"username":"张三"}
     * 返回结果示例:
     * [
     * {
     * "applyTime": "2022-05-13 15:15:45.0",
     * "department": "技术部",
     * "endTime": "2022-05-19 15:00:00.0",
     * "outApplyId": 1,
     * "outReason": "部门外派",
     * "name": "zhangsan",
     * "startTime": "2022-05-18 09:00:00.0",
     * "workId": "100005"
     * }
     * ]
     */
    public void checkOutApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String username = jsonObject.get("username").toString();
        Employee employee = employeeService.getEmployeeByUsername(username);
        String name = employee.getName();
        String workId = employee.getWorkId();
        Integer deptId = employee.getDeptId();
        String deptName = departmentService.selectDeptNameById(deptId);

        List<Integer> outApplyIdList = outApplyService.getOutApplyIdListByUsername(username);

        List<OutApplyCheck> outApplyCheckList = new ArrayList<>();
        for (Integer outApplyId : outApplyIdList) {
            OutApplyCheck outApplyCheck = new OutApplyCheck();
            OutApply outApply = outApplyService.getOutApplyByOutApplyId(outApplyId);
            String applyTime = outApply.getApplyTime();
            String startTime = outApply.getStartTime();
            String endTime = outApply.getEndTime();
            String outReason = outApply.getOutReason();
            String outCategory = outApply.getOutCategory();

            outApplyCheck.setOutApplyId(outApplyId);
            outApplyCheck.setName(name);
            outApplyCheck.setWorkId(workId);
            outApplyCheck.setDepartment(deptName);
            outApplyCheck.setApplyTime(applyTime);
            outApplyCheck.setStartTime(startTime);
            outApplyCheck.setEndTime(endTime);
            outApplyCheck.setOutReason(outReason);

            outApplyCheckList.add(outApplyCheck);
        }

        String jsonString = JSON.toJSONString(outApplyCheckList);

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

}
